package server.messageContent;

import server.AccountServiceImpl;
import common.SerializationManager;
import common.ServiceErrorException;
import common.ServiceResponseEnum;
import common.data.ResponseTypeEnum;
import common.interfaces.MessageContent;

public class BalanceEnquiryReplyMsgContent implements MessageContent {
	private byte[] data;

	public BalanceEnquiryReplyMsgContent(ServiceResponseEnum response,
			long accId) throws ServiceErrorException {
		String responseString = null;
		ResponseTypeEnum responseTypeEnum;

		switch (response) {
		case NON_EXISTING_ACCOUNT:
			responseString = response.getMsg();
			responseTypeEnum = ResponseTypeEnum.ERROR;
			break;

		case WRONG_PASSWORD:
			responseString = response.getMsg();
			responseTypeEnum = ResponseTypeEnum.ERROR;
			break;
		case SUCCESS_BALANCE_ENQUIRY:
			responseString = response.getMsg()
					+ AccountServiceImpl.getCurrent().BalanceEnquiry(accId);
			responseTypeEnum = ResponseTypeEnum.SUCCESSFUL;
			break;
		default:
			responseTypeEnum = ResponseTypeEnum.ERROR;
			System.out.println("Response is unexpected" + response.getMsg());
			break;
		}

		byte[] responseTypeEnumStream = SerializationManager.getCurrent()
				.fromResponseTypeEnum(responseTypeEnum);

		byte[] responseStream = SerializationManager.getCurrent().fromString(
				responseString);
		byte[] responseLengthStream = SerializationManager.getCurrent()
				.fromInt(responseStream.length);

		int currentIndex = 0;

		if (responseTypeEnum == ResponseTypeEnum.ERROR) {
			this.data = new byte[responseTypeEnumStream.length
			                     + responseLengthStream.length
			                     + responseStream.length];
			System.arraycopy(responseTypeEnumStream, 0, data, currentIndex, responseTypeEnumStream.length);
			currentIndex += responseTypeEnumStream.length;
			
			System.arraycopy(responseLengthStream, 0, data, currentIndex,
					responseLengthStream.length);
			currentIndex += responseLengthStream.length;
			
			System.arraycopy(responseStream, 0, data, currentIndex,
					responseStream.length);
			currentIndex += responseStream.length;
		} else { // serialize response object: current balance
			byte[] responseObjectStream = SerializationManager.getCurrent()
					.fromFloat(
							AccountServiceImpl.getCurrent().BalanceEnquiry(
									accId));
			byte[] responseObjectLengthStream = SerializationManager
					.getCurrent().fromInt(responseObjectStream.length);
			
			this.data = new byte[responseTypeEnumStream.length
			                     + responseLengthStream.length
			                     + responseStream.length
			                     + responseObjectLengthStream.length
			                     + responseObjectStream.length];
			
			System.arraycopy(responseTypeEnumStream, 0, data, currentIndex, responseTypeEnumStream.length);
			currentIndex += responseTypeEnumStream.length;
			
			System.arraycopy(responseLengthStream, 0, data, currentIndex,
					responseLengthStream.length);
			currentIndex += responseLengthStream.length;
			
			System.arraycopy(responseStream, 0, data, currentIndex,
					responseStream.length);
			currentIndex += responseStream.length;
			
			System.arraycopy(responseObjectLengthStream, 0, data, currentIndex, responseObjectLengthStream.length);
			currentIndex += responseObjectLengthStream.length;
			
			System.arraycopy(responseObjectStream, 0, data, currentIndex, responseObjectStream.length);
			currentIndex += responseObjectStream.length;			
		}
	}

	public byte[] getByte() {
		return this.data;
	}

	public int getLength() {
		return this.data.length;
	}
}
